<?php include('includes/start_session.php') ?>
<?php include('includes/login_required.php') ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
	<title>e-class register to courses</title>

	<link rel="stylesheet" href="styles/site.css" type="text/css" />
	<link rel="stylesheet" href="styles/less_padding.css" type="text/css" />
	
	<script type="text/javascript" src="scripts/scripts.js"></script>
</head>
<body onload="same_height()">
	<?php include('includes/header.php') ?>
	<div id="content" class="content_padding">
		<?php
			if(isset($_GET['message']) && $_GET['message'] == '1')
				echo '<div class="center_align"><span class="error">Δεν επιλέξατε κανένα μάθημα</span></div>';
			else if(isset($_GET['message']) && $_GET['message'] == '2')
				echo '<div class="center_align"><span class="success">Η εγγραφή έγινε με επιτυχία</span></div>';
		?>
		<h3>Δήλωση μαθημάτων</h3>
		<div class="center_align">
			<p>Επιλέξτε κατηγορία:<br /><i>(επιλέξτε μαθήματα απο όσες κατηγορίες θέλετε)</i></p>
			<select multiple="multiple" id="category_list" onchange="show_category('category_list', 'message')">
				<?php
					include('db-api/txt-db-api.php');
					include('includes/course_info.php');
					
					$db = new Database('Eclass');
					$rs = $db->executeQuery('SELECT Name FROM Categories');
					while($rs->next())
					{
						list($cat) = $rs->getCurrentValues();
						echo '<optgroup label="' . $cat . '">';
						
						$rs1 = $db->executeQuery('SELECT Name FROM SubCategories WHERE CategoryName = \'' .
							$cat . '\'');
						while($rs1->next())
						{
							list($subcat) = $rs1->getCurrentValues();
							echo '<option value="' . $subcat . '">' . $subcat . '</option>';
						}
						
						echo '</optgroup>';
					}
				?>
			</select>
		</div>
		<div id="categories_div">
			<form action="register_to_courses.php" method="post" id="register_form" class="hidden">
			<h3 id="message"></h3>
			<?php
				$_SESSION['previous_page'] = 'register_courses.php';
				$subcats = $db->executeQuery('SELECT Name FROM SubCategories');
				while($subcats->next())
				{
					list($subcat) = $subcats->getCurrentValues();
					echo '<ul id="' . $subcat . '" class="hidden_course_list">';
					
					$isUser = $db->executeQuery('SELECT CourseId FROM UsersCourses WHERE UserName = \'' . $_SESSION['username'] . '\'');
					$isProf = $db->executeQuery('SELECT CourseId FROM ProfessorsCourses WHERE ProfessorName = \'' . $_SESSION['username'] . '\'');
					$query = 'SELECT Id, Name, Description FROM Courses WHERE SubCategory = \'' .
						$subcat . '\'';
					while($isUser->next())
					{
						list($id) = $isUser->getCurrentValues();
						$query = $query . ' AND Id != ' . $id;
					}
					while($isProf->next())
					{
						list($id) = $isProf->getCurrentValues();
						$query = $query . ' AND Id != ' . $id;
					}
					$courses = $db->executeQuery($query);
					$first = true;
					if($courses->next() == false)
						echo 'Δεν υπάρχουν μαθήματα στα οποία μπορείτε να εγγραφείτε';
					else
					{
						do
						{
							list($id, $name, $description) = $courses->getCurrentValues();
							if($first == true)
							{
								echo '<li class="first_course">';
								$first = false;
							}
							else
								echo '<li>';
						
							echo '<table><tr><td valign="middle">';
							echo '<input type="checkbox" name="courses[]" value="' . $id . '"/>';
							echo '</td><td>';
							echo '<div class="course">';
						
							echo '<a href="course.php?id=' . $id . '" class="title">' . $name . '</a>';
							ShowProfessors($id);

							echo '</div></td>';
							echo '</tr></table></li>';
						} while($courses->next());
					}
					
					echo '</ul>';
				}
			?>
			<div class="right_align">
				<a href="#" class="course_anchor">Επιστροφή στην κορυφή</a>
				<br />
				<br />
				<input type="submit" value="Παρακολούθηση" />
			</div>
			</form>
			</div>
		</div>
	<?php include('includes/footer.php') ?>
</body>
</html>
